package filters

import (
	"encoding/json"
	"xemo.top/docker/docker/api/types/versions"
)

// Args 存储键到一组多个值的映射。
// Args stores a mapping of keys to a set of multiple values.
type Args struct {
	fields map[string]map[string]bool
}

// Len 返回映射中的键数
// Len returns the number of keys in the mapping
func (args Args) Len() int {
	return len(args.fields)
}

// ToJSON returns the Args as a JSON encoded string
func ToJSON(a Args) (string, error) {
	if a.Len() == 0 {
		return "", nil
	}
	buf, err := json.Marshal(a)
	return string(buf), err
}

// ToParamWithVersion 将Args编码为JSON字符串。如果version小于1.22，则编码格式将使用较旧的旧版格式，其中值是字符串列表而不是集合。
// ToParamWithVersion encodes Args as a JSON string.If version is less than 1.22 then the encoded format will use an older legacy format where the values are a list of strings, instead of a set.
//
// Deprecated: Use ToJSON
func ToParamWithVersion(version string, a Args) (string, error) {
	if a.Len() == 0 {
		return "", nil
	}
	if version != "" && versions.LessThan(version, "1.22") {
		buf, err := json.Marshal(convertArgsToSlice(a.fields))
		return string(buf), err
	}
	return ToJSON(a)
}

func convertArgsToSlice(f map[string]map[string]bool) map[string][]string {
	m := map[string][]string{}
	for k, v := range f {
		values := []string{}
		for kk := range v {
			if v[kk] {
				values = append(values, kk)
			}
		}
		m[k] = values
	}
	return m
}
